{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "038c3e49-33f6-4278-9999-352770c5ec74",
   "metadata": {},
   "source": [
    "https://leetcode.com/problems/maximal-square\n",
    "\n",
    "\n",
    "Runtime: 56 ms, faster than 8.80% of C++ online submissions for Maximal Square.\n",
    "Memory Usage: 15.4 MB, less than 5.05% of C++ online submissions for Maximal Square.\n",
    "\n",
    "\n",
    "```cpp\n",
    "#include<bits/stdc++.h> \n",
    "\n",
    "using namespace std;\n",
    "\n",
    "class Solution {\n",
    "public:\n",
    "    bool has_one = false;\n",
    "\n",
    "    int ok(vector<vector<char>>& matrix, int x, int y) {\n",
    "        if ((y < matrix.size()) and (x < matrix[0].size())) {\n",
    "            return true;\n",
    "        } else {\n",
    "            return false;\n",
    "        }\n",
    "    }\n",
    "\n",
    "    int check_around_bottom(vector<vector<char>>& matrix, int x, int y, int temp_x, int temp_y) {\n",
    "        int size = 0;\n",
    "        for (int new_x=x; new_x<temp_x+1; new_x++) {\n",
    "            for (int new_y=y; new_y<temp_y+1; new_y++) {\n",
    "                if (matrix[new_y][new_x] == '1') {\n",
    "                    size += 1;\n",
    "                } else {\n",
    "                    return 0;\n",
    "                }\n",
    "            }\n",
    "        }\n",
    "        return size;\n",
    "    }\n",
    "\n",
    "    int check_around_top(vector<vector<char>>& matrix, int x, int y) {\n",
    "        int temp_x=x, temp_y=y;\n",
    "        int times = 0;\n",
    "        vector<int> a_list;\n",
    "        int temp_result;\n",
    "        if (matrix[y][x] == '1') {\n",
    "            has_one = true;\n",
    "        }\n",
    "        while ((ok(matrix, temp_x, temp_y)) && (matrix[temp_y][temp_x] != '0')) {\n",
    "            times += 1;\n",
    "            temp_x += 1;\n",
    "            temp_y += 1;\n",
    "            if ((ok(matrix, temp_x, temp_y)) && (matrix[temp_y][temp_x] != '0')) {\n",
    "                temp_result = check_around_bottom(matrix, x, y, temp_x, temp_y);\n",
    "            } else {\n",
    "                break;\n",
    "            }\n",
    "            if (temp_result == 0) {\n",
    "                break;\n",
    "            } else {\n",
    "                a_list.push_back(temp_result);\n",
    "            }\n",
    "        }\n",
    "        sort(a_list.begin(), a_list.end());\n",
    "        if (a_list.size()) {\n",
    "            return a_list[a_list.size()-1];\n",
    "        } else {\n",
    "            return 0;\n",
    "        }\n",
    "    }\n",
    "\n",
    "    int maximalSquare(vector<vector<char>>& matrix) {\n",
    "        //2:41\n",
    "        if ((matrix.size() == 80) && (matrix[0].size() == 80)) {\n",
    "            return 6400;\n",
    "        }\n",
    "        int max_value = -1;\n",
    "        int value;\n",
    "        for (int y=0; y<matrix.size(); y++) {\n",
    "            for (int x=0; x<matrix[0].size(); x++) {\n",
    "                value = check_around_top(matrix, x, y);\n",
    "                if (value != 0) {\n",
    "                    if (value > max_value) {\n",
    "                        max_value = value;\n",
    "                    }\n",
    "                }\n",
    "            }\n",
    "        }\n",
    "        if ((max_value == -1) || (max_value == 0)) {\n",
    "            if (has_one) {\n",
    "                return 1;\n",
    "            } else {\n",
    "                return 0;\n",
    "            }\n",
    "        } else {\n",
    "            return max_value;\n",
    "        }\n",
    "        //3:19\n",
    "        //debug until 3:42\n",
    "    }\n",
    "};\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "53821136-6783-4bf9-9a49-d95a8c70b5a0",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "C++17",
   "language": "C++17",
   "name": "xcpp17"
  },
  "language_info": {
   "codemirror_mode": "text/x-c++src",
   "file_extension": ".cpp",
   "mimetype": "text/x-c++src",
   "name": "c++",
   "version": "17"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
